﻿using Newtonsoft.Json;
using SheXiangTou.Beans.LocalModel;
using SheXiangTou.Services;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace SheXiangTou.Controller
{
    public class DataBaseControl
    {
        public static void Add_EnterRecord_And_MQ(TbRzAlarm tbRzAlarm)
        {
            Task.Run(() =>
            {
                TEnterRecord enterRecord = new TEnterRecord
                {
                    Phone = tbRzAlarm.CardNum,
                    Mask = tbRzAlarm.Mask.ToString(),
                    Temperature = tbRzAlarm.UserTemperature,
                    UserImg = tbRzAlarm.HeadPicture,
                    ProjectName = "紫蓬山",
                    ProjectId = 1,
                    PassType = tbRzAlarm.Inout == 1 ? "进" : "出",
                    PassTime = DateTime.Now,
                    CreateTime = DateTime.Now,
                    UserName = LocalDatabaseService.GetUserName_ByPhone(tbRzAlarm.CardNum)
                };
                if (LocalDatabaseService.AddEnterRecord(enterRecord) > 0)//成功导入数据库 进行队列下发人员进出信息
                {
                    ToRenzhaEnter_Cloud toRenzhaEnter_Local = new ToRenzhaEnter_Cloud
                    {
                        userName = enterRecord.UserName,
                        phone = enterRecord.Phone,
                        mask = enterRecord.Mask,
                        temperature = enterRecord.Temperature,
                        userImg = tbRzAlarm.HeadPicture,
                        projectName = enterRecord.ProjectName,
                        projectId = enterRecord.ProjectId,
                        passType = enterRecord.PassType,
                        passTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    };

                    ToRenzhaEnter_Cloud toRenzhaEnter_Cloud = new ToRenzhaEnter_Cloud
                    {
                        userName = enterRecord.UserName,
                        phone = enterRecord.Phone,
                        mask = enterRecord.Mask,
                        temperature = enterRecord.Temperature,
                        userImg = tbRzAlarm.OssPicture,
                        projectName = enterRecord.ProjectName,
                        projectId = enterRecord.ProjectId,
                        passType = enterRecord.PassType,
                        passTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                        createTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
                    };
                    RabbitMqService.Publish_Local_Message_Async("1", ConfigerService.Direct_Screen_Exchange, ConfigerService.Local_Passuser_Queue);//发送消息到本地队列
                    RabbitMqService.Publish_Cloud_Message_Async(JsonConvert.SerializeObject(toRenzhaEnter_Cloud), ConfigerService.Cloud_Info_Exchange, ConfigerService.Info_User_Queue);//发送消息到远程队列
                }
            });
        }
    }
}
